package com.zhz.system.strategy.handler;

import com.zhz.common.operator.OperatorRouter;
import com.zhz.common.utils.ConvertUtils;
import com.zhz.model.system.contract.dto.PermissionApiDTO;
import com.zhz.model.system.contract.param.PermissionApiQueryParam;
import com.zhz.system.contract.enums.PermissionTypeEnum;
import com.zhz.system.contract.request.PermissionSaveRequest;
import com.zhz.system.contract.request.PermissionUpdateRequest;
import com.zhz.system.contract.response.PermissionFindByIdResponse;
import com.zhz.system.repository.service.PermissionApiService;
import com.zhz.system.strategy.PermissionTypeOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 权限类型为api的操作
 *
 * @author zhouhengzhe
 * @date 2023/3/30
 */
@Component
public class PermissionApiOperator implements PermissionTypeOperator {

    @Autowired
    private PermissionApiService permissionApiService;

    /**
     * Operator的名称, 同一类型的Operator的路由器{@link OperatorRouter}能够根据该值路由到当前的Operator
     *
     * @return route key
     */
    @Override
    public Integer getName() {
        return PermissionTypeEnum.PERMISSION_API.getCode();
    }

    /**
     * 添加对应的数据--添加权限api
     *
     * @param permissionSaveRequest 权限保存请求
     * @param pid
     */
    @Override
    public void add(PermissionSaveRequest permissionSaveRequest, String pid) {
        PermissionApiDTO permissionApiDTO = ConvertUtils.sourceToTarget(permissionSaveRequest, PermissionApiDTO.class)
                .setPid(pid);
        permissionApiService.save(permissionApiDTO);
    }

    /**
     * 修改对应的数据--修改权限api
     *
     * @param permissionUpdateRequest 权限修改请求
     * @param pid                     父id
     */
    @Override
    public void update(PermissionUpdateRequest permissionUpdateRequest, String pid) {
        PermissionApiDTO permissionApiDTO = permissionApiService.getOne(PermissionApiQueryParam.builder().pid(pid).build());
        permissionApiDTO.setApiMethod(permissionUpdateRequest.getApiMethod())
                .setApiUrl(permissionUpdateRequest.getApiUrl())
                .setApiLevel(permissionUpdateRequest.getApiLevel())
                .setPid(pid);
        permissionApiService.update(permissionApiDTO);
    }

    @Override
    public PermissionFindByIdResponse findById(PermissionFindByIdResponse permissionFindByIdResponse, String id) {
        PermissionApiDTO permissionApiDTO = permissionApiService.getOne(PermissionApiQueryParam.builder().pid(id).build());
        if (Objects.nonNull(permissionApiDTO)) {
            permissionFindByIdResponse
                    .setApiLevel(permissionApiDTO.getApiLevel())
                    .setApiMethod(permissionApiDTO.getApiMethod())
                    .setApiUrl(permissionApiDTO.getApiUrl());
        }
        return permissionFindByIdResponse;
    }

    /**
     * 根据业务key删除
     *
     * @param id 主键
     */
    @Override
    public void deleteById(String id) {
        permissionApiService.deleteByKey(id);
    }
}
